// video_player_widget.dart

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:sonora/bloc/video/sj_video_bloc.dart';
import 'package:sonora/bloc/video/sj_video_state.dart';
import 'package:sonora/models/sj_media_data.dart';
import 'package:sonora/shared/lib/constants/sj_colors.dart';
import 'package:sonora/utils/sj_text_style.dart';
import 'package:sonora/views/appbar/sj_appbar.dart';
import 'package:video_player/video_player.dart';

class SjVideoPlayerWidget extends StatefulWidget {
  final String assetPath;
  final bool isPlaying;
  final VoidCallback onTap;
  final VoidCallback onMoreTap;
  final SjMediaData mediaData;
  final VoidCallback onLikeTap;
  final VoidCallback? userInfoTap;
  final void Function(int currentIndex) onPlay;
  final VideoPlayerController? videoController;

  const SjVideoPlayerWidget(
      {super.key,
      required this.assetPath,
      required this.isPlaying,
      required this.onTap,
      required this.onMoreTap,
      required this.mediaData,
      required this.onLikeTap,
      this.userInfoTap,
      required this.onPlay,
      required this.videoController});

  @override
  State<SjVideoPlayerWidget> createState() => _SjVideoPlayerWidgetState();
}

class _SjVideoPlayerWidgetState extends State<SjVideoPlayerWidget> {
  late bool mediaPlay = true;

  @override
  void initState() {
    super.initState();
  }

  @override
  void didUpdateWidget(SjVideoPlayerWidget oldWidget) {
    super.didUpdateWidget(oldWidget);
    if (widget.isPlaying != oldWidget.isPlaying) {}
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        extendBodyBehindAppBar: true, // 使内容延伸到导航
        appBar: SjAppbar(
            backgroundColor: Colors.transparent, // 设置导航栏背景为透明
            elevation: 0, // 去除导航栏阴影
            actions: [
              IconButton(
                icon: Image.asset('assets/images/nav_more.png'), // 右侧按钮
                onPressed: widget.onMoreTap,
              ),
            ],
            title: InkWell(
                onTap: () {
                  widget.userInfoTap?.call();
                },
                child: Row(
                  children: [
                    Container(
                      height: 46,
                      padding: const EdgeInsets.symmetric(horizontal: 12),
                      decoration: const BoxDecoration(
                          borderRadius: BorderRadius.all(Radius.circular(23.0)),
                          color: Color(0x40000000)),
                      child: Row(mainAxisSize: MainAxisSize.min, children: [
                        Container(
                          width: 32, // Diameter (2 * radius) = 2 * 15
                          height: 32, // Diameter (2 * radius) = 2 * 15
                          decoration: BoxDecoration(
                            shape: BoxShape.circle,
                            image: DecorationImage(
                              image: NetworkImage(
                                  widget.mediaData.broadCaster?.avatar ??
                                      ''), // Replace with your avatar image URL
                              fit: BoxFit.cover,
                            ),
                            border: Border.all(
                              color: Colors.black, // Border color
                              width: 1, // Border width
                            ),
                            boxShadow: const [
                              BoxShadow(
                                color: SjColors
                                    .themeColor, // Shadow color and opacity
                                offset: Offset(1, 1), // Shadow offset (x, y)
                              ),
                            ],
                          ),
                        ),
                        const SizedBox(
                          width: 4,
                        ),
                        Text(
                          widget.mediaData.broadCaster?.nickname ?? '',
                          style: SjTextStyle.customFontWhite16,
                        )
                      ]),
                    )
                  ],
                ))),
        body: BlocBuilder<SjVideoBloc, SjVideoState>(builder: (context, state) {
          SjMediaData mediaData = state.mediaList!
              .where((element) => element.mediaUrl! == widget.assetPath)
              .toList()
              .first;
          return GestureDetector(
              behavior: HitTestBehavior.opaque, // 允许接受点击事件
              onTap: () {
                widget.onTap.call();
              },
              child: Stack(
                children: [
                  VideoPlayer(widget.videoController!),
                  Visibility(
                    visible:
                        widget.videoController?.value.isBuffering == true,
                    child: SizedBox(
                      width: MediaQuery.of(context).size.width,
                      height: MediaQuery.of(context).size.height,
                      child: const Center(
                        child:
                            CircularProgressIndicator(), // 或使用 LinearProgressIndicator()
                      ),
                    ),
                  ),
                  Container(
                    decoration: const BoxDecoration(
                        gradient: LinearGradient(
                            colors: [Color(0x00000000), Color(0xFF000000)],
                            begin: Alignment.center,
                            end: Alignment.bottomCenter)),
                  ),
                  Center(
                    child: widget.isPlaying
                        ? const SizedBox()
                        : Image.asset('assets/images/video_play_btn.png'),
                  ),
                  Positioned(
                    left: 20,
                    right: 20,
                    bottom: 12,
                    child: Text(
                      widget.mediaData.title ?? '',
                      style: SjTextStyle.customFontWhite16,
                    ),
                  ),
                  Positioned(
                    right: 20,
                    bottom: 108,
                    child: InkWell(
                        onTap: widget.onLikeTap,
                        child: (mediaData.isLike != null &&
                                mediaData.isLike == true)
                            ? Image.asset('assets/images/video_like.png')
                            : Image.asset('assets/images/video_un_like.png')),
                  )
                ],
              ));
        }));
  }

  @override
  void dispose() {
    // widget.controller?.dispose();
    super.dispose();
  }
}
